/**
 * 
 */
$(document).ready(function () {
	/**
	 * Redirige l'utilisateur si il est déjà connectée
	 */
	if(auth != null && auth.connected == false) {
		window.location.href = "/index.html";
	}
	
	$.ajax('user',{
		type:'GET',
		dataType: "json",
		data: {
			cmd:'listeUser',
        },
	}).done(function(data, testStatus, jqXHR){
		for(var user in data)
		{
			$("#userList").append(
			"<tr><td>" +
			data[user].nom + 
			" " +
			data[user].prenom +
			"</td><td>" +
			data[user].login +
			"</td><td>" +
			data[user].dateNaissance +
			"</td><td>" +
			data[user].lastConnection +
			"</td><td>" +
			data[user].duration +
			"</td><td>" +
			"<a class=\"btn btn-primary btn-block deleteUser\" href=\"\" id=\"" +
			data[user].id +
			"\" >Supprimer</a>" +
			"</td></tr>"		
			);
		};
	}).fail(function(jqXHR, textStatus, errorThrown) {
        	setAlertMessage('Erreur!',errorThrown+ ' (' + textStatus +')','danger');
	});
	
	$("#userList").delegate(".deleteUser", "click", function() {
		var idBtn = $(this).attr("id");
		$.ajax({
			url: 'user' + '?' + $.param({"cmd": "deleteUserQueue", "id": $(this).attr("id")}),
            type: "DELETE",
        }).done(function(data, textStatus, jqXHR) {
        	setAlertMessage('Succès!','User supprimé','success');
        	$("#" + idBtn).closest("tr").remove();
        }).fail(function(jqXHR, textStatus, errorThrown) {
        	setAlertMessage('Erreur!',errorThrown+ ' (' + textStatus +')','danger');
        });
		return false;
	});
});